package com.xxwy.springboot.routerfunction.routers;

import com.xxwy.springboot.routerfunction.handler.UserHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;

/**
 * @author xxwy
 * on 2018/8/17 0017
 * 类似dispacherservlet
 */
@Configuration
public class AllRouters {

    @Bean
    RouterFunction<ServerResponse> userRouter(UserHandler userHandler) {
        return RouterFunctions.nest(
                //同一的前缀
                RequestPredicates.path("/user"),
                //添加路由和他匹配的方法
                RouterFunctions.route(
                        RequestPredicates.GET("/"),
                        userHandler::getAllUser))
                //添加路由和他匹配的方法
                .andRoute(
                        RequestPredicates.POST("/"),
                        userHandler::insertUser)
                .andRoute(
                        RequestPredicates.DELETE("/{id}"),
                        userHandler::deletetUser
                );
    }


}
